#!/bin/sh
#
# OracleAppRemount.sh: shell script to automate cloning and bring up an Oracle database using backup image copies
#
#usage: OracleAppRemount.sh <New database sid name> <Oracle Home path> <Backup location mount path> <log mount path> <timestamp yyyymmddhh24mi>"
set +x
nsid=$1
oracleHome=$2
backupLocation=$3
#psid=$4

dataDir=$backupLocation

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ];then
    echo "missing env LB_ROOT"
    exit 1
fi

. ${scriptsRoot}/oracle/oracleclone/OracleAppMountComFunc.sh

BASEDIR=$(dirname $0)
echo $BASEDIR
cd $BASEDIR
# correctly set the ORACLE_BASE AND ORACLE_HOME  for your environment
export ORACLE_HOME=$oracleHome
export ORACLE_BASE=$dataDir
export PATH=$ORACLE_HOME/bin:$PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib
export LD_LIBRARY_PATH

if [ ! -d $ORACLE_HOME ]; then
   echo "could not find ORACLE_HOME "
   exit 1
fi

if [ ! -d $ORACLE_BASE ]; then
   echo "could not find dataDir"
   exit 1
fi

if [ ! -d $backupLocation ]; then
   echo "could not find backup location"
   exit 1
fi
 
if [ ! -z "$psid" ]; then
   echo "missing previous SID"
   exit 1
fi

sqlplus=$ORACLE_HOME/bin/sqlplus

samemp="true"
psid=`get_teardown_sid $dataDir`
initfile=$dataDir/init$psid.ora
tempinitfile=$dataDir/init$psid_temp.ora

pmp=`get_prev_mountpoint $dataDir`
if [ $? = 0 ]; then
    if [ "$pmp" = "$dataDir" ]; then
       echo "using same mount point"
    else
       echo "using different mount point"
       samemp="false"
    fi
fi


ORACLE_SID=$nsid
export ORACLE_SID

if [ "$psid" = "$nsid" ]; then
    echo "remount with same SID"
    cp $dataDir/init$nsid.ora $ORACLE_HOME/dbs/
    if [ "$samemp" = "true" ]; then
       echo "remount with same SID $nsid using same mountpoint"
       # copy pfile to ORACLE_HOME/dbs, start database
       cp $initfile $ORACLE_HOME/dbs/
ORACLE_SID=$nsid
export ORACLE_SID

$sqlplus / as sysdba << EOF1
shutdown immediate;
startup mount;
alter database end backup;
alter database open;
exit;
EOF1
    else
       echo "remount with same SID $nsid using different mountpoint"
       # same SID, different mp, change dataDir in pfile
       #sed "s/$osid/$nsid/g" $initfile > $tempinitfile
       
       #update previous mount point info
       echo "$dataDir" > $dataDir/pmpkeep
     
# bug 25971 
     #  sed "s=${pmp}=${dataDir}=g" $initfile > $tempinitfile
     #  mv $tempinitfile $initfile

         sed "s=${pmp}/admin=${dataDir}/admin=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile
         sed "s=${pmp}/oradata=${dataDir}/oradata=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile
         sed "s=${pmp}/flash_recovery_area=${dataDir}/flash_recovery_area=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile

       cp $initfile $ORACLE_HOME/dbs/
export ORACLE_SID=$psid
$sqlplus / as sysdba << EOF2
startup nomount force pfile='$initfile';
create spfile='$ORACLE_HOME/dbs/spfile$psid.ora' from pfile='$initfile';
shutdown immediate;
startup mount;
@${scriptsRoot}/oracle/oracleclone/renameDatafile.sql $dataDir $psid;
@${scriptsRoot}/oracle/oracleclone/renamedf_generated.sql
alter database open;
exit;
EOF2
    fi
    cp $dataDir/setdb_$nsid ${scriptsRoot}/oracle/oracleclone/
	cp $dataDir/orapw$nsid $ORACLE_HOME/dbs/orapw$nsid
else
    # Different SID for remount
    echo "remount with different SID"
    if [ "$samemp" = "true" ]; then
       echo "remount with SID $nsid to replace $psid using same mountpoint"
       cp $initfile $ORACLE_HOME/dbs/

#       cd ${scriptsRoot}/oracle/oracleclone
#       . ./setdb_$nsid
export ORACLE_SID=$psid
$sqlplus / as sysdba << EOF3
startup nomount force pfile='$initfile';
create spfile='$ORACLE_HOME/dbs/spfile$psid.ora' from pfile='$initfile';
shutdown immediate;
exit;
EOF3
${scriptsRoot}/oracle/oracleclone/renameDatabase.sh $nsid $psid

        if [ $? != 0 ]; then
          echo "failed to rename database to $nsid"
          exit 1
        fi
        
    #update setdb_ initora files
    cp $dataDir/setdb_$psid $dataDir/setdb_$nsid
    cp $dataDir/setdb_$nsid ${scriptsRoot}/oracle/oracleclone/
    #mv $dataDir/init$psid.ora $dataDir/init$nsid.ora
    cp $ORACLE_HOME/dbs/init$nsid.ora $dataDir/init$nsid.ora
    rm $dataDir/init$psid.ora
    mv $dataDir/orapw$psid $dataDir/orapw$nsid
    cp $dataDir/orapw$nsid $ORACLE_HOME/dbs/
    
    else
         echo "remount with SID $nsid to replace $psid using different mountpoint"
        #sed "s/$osid/$nsid/g" $initfile  $initfile > $tempinitfile
        # sed "s=${pmp}=${dataDir}=g"  $initfile > $tempinitfile
        # mv $tempinitfile $initfile

# bug 25971
         sed "s=${pmp}/admin=${dataDir}/admin=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile
         sed "s=${pmp}/oradata=${dataDir}/oradata=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile
         sed "s=${pmp}/flash_recovery_area=${dataDir}/flash_recovery_area=g" $initfile > $tempinitfile
         mv $tempinitfile $initfile

         cp $initfile $ORACLE_HOME/dbs/
export ORACLE_SID=$psid
$sqlplus / as sysdba << EOF5
startup nomount force pfile='$initfile';
create spfile='$ORACLE_HOME/dbs/spfile$psid.ora' from pfile='$initfile';
shutdown immediate;
startup mount;
@${scriptsRoot}/oracle/oracleclone/renameDatafile.sql $dataDir $psid;
@${scriptsRoot}/oracle/oracleclone/renamedf_generated.sql
alter database open;
shutdown immediate;
exit;
EOF5
${scriptsRoot}/oracle/oracleclone/renameDatabase.sh $nsid $psid

        if [ $? != 0 ]; then
          echo "failed to rename database to $nsid after changing datafile path"
          exit 1
        fi   
    fi
    tmpsetdbf=$dataDir/setdb_$psid_tmp
    sed "s/$psid/$nsid/g" $dataDir/setdb_$psid > $tmpsetdbf
    mv $tmpsetdbf $dataDir/setdb_$psid

    cp $dataDir/setdb_$psid ${scriptsRoot}/oracle/oracleclone/
    mv ${scriptsRoot}/oracle/oracleclone/setdb_$psid ${scriptsRoot}/oracle/oracleclone/setdb_$nsid
    cp $dataDir/orapw$psid $ORACLE_HOME/dbs/
    mv $ORACLE_HOME/dbs/orapw$psid $ORACLE_HOME/dbs/orapw$nsid
    
    #update previous mount point info
    echo "$dataDir" > $dataDir/pmpkeep
    
    #update setdb_ initora files
    mv $dataDir/setdb_$psid $dataDir/setdb_$nsid
    #mv $dataDir/init$psid.ora $dataDir/init$nsid.ora
    cp $ORACLE_HOME/dbs/init$nsid.ora $dataDir/init$nsid.ora
    rm $dataDir/init$psid.ora
    mv $dataDir/orapw$psid $dataDir/orapw$nsid
    
fi

# update /etc/oratab for reprotect
oratabentry=$nsid":"$oracleHome":Y"
echo "$oratabentry" >> /etc/oratab

exit 0
